<?php

namespace app\index\controller;
use app\common\model\tag\Question as QuestionModel;

use think\Validate;
/**
 * 问卷调查的时候
 */
class Question extends \app\common\controller\AuthBase {

    public function __construct() {
        parent::__construct();
    }

    /**
     * 获取有没有对应的卡券记录
     */
    public function getQuestionRecord(){
        
        $params = $this->input(); 
        $rule = [
            "company_id" => 'require|number|max:11',
            "condition" => 'require|max:50',
        ]; 
        $validate = new Validate($rule);
        if(!$validate->check($params)){
            return $this->output([], $validate->getError());
        }
        
        $questionModel = new QuestionModel();
        $questionModel->setTrueTable("{$params['company_id']}");
        
        $questionOne = $questionModel->getOne('*',$params['condition']);
        
        return $this->output($questionOne, 'success');
    }
    
    
    /**
     * 插入问卷
     */
    public function saveQuestion(){
        
        $params = $this->input(); 
        $rule = [
            "company_id"  => 'require|number|max:11',
            'vip_id'      => 'max:50',
            'come_ts'     => 'number',
            'come_from'   => 'max:20',
            "question_id" => 'max:50',
            'mobile'      => 'max:20',
            'gender'      => 'max:5',
            'birthday'    => 'date',
            'agree_1'     => 'number',
            'agree_2'     => 'number',
            'agree_3'     => 'number',
            'agree_4'     => 'number',
            'agree_5'     => 'number',
            'agree_6'     => 'number',
            'agree_7'     => 'number',
            'agree_8'     => 'number',
            'add_ts'      => 'number',
        ]; 
        $validate = new Validate($rule);
        if(!$validate->check($params)){
            return $this->output([], $validate->getError());
        }
        
        $questionModel = new QuestionModel();
        $questionModel->setTrueTable("{$params['company_id']}");
        
        //如果问题id为空，那么就是新增
        if(empty($params['question_id'])){
            $questionId = $questionModel->addOne($params);
            return $this->output(['question_id'=>$questionId], 'success');
        }
        
        $questionModel->updateOneByQuestionId($params);
        return $this->output(true, 'success');
    }
}
